package com.hmkj.core.api.etonepay;

import com.alibaba.fastjson.JSON;
import com.etonepay.b2c.entity.BackForm;
import com.etonepay.b2c.entity.Constant;
import com.etonepay.b2c.entity.PayForm;
import com.etonepay.b2c.entity.QueryForm;
import com.etonepay.b2c.exception.EtonepayException;
import com.etonepay.b2c.service.impl.EtonepayServiceImpl;
import com.etonepay.b2c.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 易通支付
 *
 * @author Vector
 * @create 2017-12-20 17:16
 */
@Slf4j
public class EtonePayUtil {
    /**
     * 支付请求地址
     */
    private static final String payURL = "http://58.56.23.89:7002/NetPay/BankSelect.action";
//    private static final String payURL = "https://cashier.etonepay.com/NetPay/BankSelect.action";
    /**
     * 查询请求地址
     */
    private static final String queryURL = "http://58.56.23.89:7002/NetPay/MerOrderQuery.action";
//    private static final String queryURL = "https://cashier.etonepay.com/NetPay/MerOrderQuery.action";
    /**
     * 交易代码
     */
    private static final String transCode = "8888";
    /**
     * 商户编号
     */
    private static final String merchantId = "888888888888888";
    /**
     * 业务代码
     */
    private static final String bussID = "100000";
    /**
     * 密钥 datakey
     */
    private static final String dataKey = "8EF53C251102A4E6";
    /**
     * 货币代码
     */
    private static final String currencyType = "156";

    /**
     * 请求支付跳转URL
     *
     * @param payForm 支付表单数据
     * @return
     */
    public static String getPayUrl(PayForm payForm) throws UnsupportedEncodingException, EtonepayException {
        // 创建接口实例，并发送表单生成url
        log.info("易通支付请求参数：{}", JSON.toJSONString(payForm));
        return new EtonepayServiceImpl().getPayURL(payForm);
    }

    /**
     * 订单查询接口
     *
     * @param queryForm
     * @return
     */
    public static List<String> query(QueryForm queryForm) {
        /**
         * 创建接口实例并请求数据
         */
        String url = new EtonepayServiceImpl().getQueryURL(queryForm);
        /**
         * 返回请求结果
         */
        // 返回查询结果，String格式，商户可以自己处理
        String resp = HttpUtil.HttpGet(url.toString());
        String[] strArr = resp.split("&");
        List<String> list = new ArrayList<>();
        for (String s : strArr) {
            list.add(s);
        }
        return list;
    }


    /**
     * 验证签名
     *
     * @param backForm
     * @return
     */
    public static boolean chackSignValue(BackForm backForm) {
        try {
            log.info("易通支付回调参数：{}", JSON.toJSONString(backForm));
            String resp = new EtonepayServiceImpl().chackSignValue(backForm);
            if (Constant.getSuccess().equals(resp)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error("验签失败，错误消息：" + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 创建QueryForm并注入数据
     *
     * @param merOrderNum
     * @param tranDate
     * @param s
     * @param s1
     * @param s2
     * @param s3
     * @param s4
     * @param s5
     * @param s6
     * @param s7
     * @param s8
     * @param s9          @return QueryForm
     */
    public static QueryForm buildForm(String merOrderNum, String tranDate, String s, String s1, String s2, String s3, String s4, String s5, String s6, String s7, String s8, String s9) {
        QueryForm queryForm = new QueryForm();
        queryForm.setQueryURL(queryURL);
        queryForm.setMerchantID(merchantId);
        queryForm.setMerOrderNum(merOrderNum);
        queryForm.setDataKey(dataKey);
        queryForm.setTranDate(tranDate);
        return queryForm;
    }

    /**
     * 创建PayForm并注入数据
     *
     * @param merOrderNum
     * @param tranAmt
     * @param sysTraceNum
     * @param orderInfo
     * @param bankId
     * @param stlmId
     * @param userId
     * @param userIp
     * @param reserver1
     * @param reserver2
     * @param reserver3
     * @param reserver4
     * @param tranDateTime
     * @param merURL
     * @param backURL
     * @return PayForm
     */

    public static PayForm buildForm(String merOrderNum, String tranAmt,
                                    String sysTraceNum, String orderInfo, String bankId, String stlmId,
                                    String userId, String userIp, String reserver1, String reserver2,
                                    String reserver3, String reserver4, String tranDateTime, String merURL, String backURL) {

        PayForm payForm = new PayForm();
        payForm.setPayURL(payURL);
        payForm.setTransCode(transCode);
        payForm.setMerchantID(merchantId);
        payForm.setBussID(bussID);
        payForm.setDataKey(dataKey);
        payForm.setCurrencyType(currencyType);
        payForm.setMerURL(merURL);
        payForm.setBackURL(backURL);
        payForm.setMerOrderNum(merOrderNum);
        payForm.setTranAmt(tranAmt);
        payForm.setSysTraceNum(sysTraceNum);
        payForm.setOrderInfo(orderInfo);
        payForm.setBankId(bankId);
        payForm.setStlmId(stlmId);
        payForm.setUserId(userId);
        payForm.setUserIp(userIp);
        payForm.setReserver1(reserver1);
        payForm.setReserver2(reserver2);
        payForm.setReserver3(reserver3);
        payForm.setReserver4(reserver4);
        payForm.setTranDateTime(tranDateTime);
        return payForm;
    }

    /**
     * 创建BackForm并注入数据
     *
     * @param transCode
     * @param merchantId
     * @param respCode
     * @param sysTraceNum
     * @param merOrderNum
     * @param orderId
     * @param bussId
     * @param tranAmt
     * @param orderAmt
     * @param bankFeeAmt
     * @param integralAmt
     * @param vaAmt
     * @param bankAmt
     * @param bankId
     * @param integralSeq
     * @param vaSeq
     * @param bankSeq
     * @param tranDateTime
     * @param payMentTime
     * @param settleDate
     * @param currencyType
     * @param orderInfo
     * @param userId
     * @param signValue
     * @return BackForm
     */

    public static BackForm buildForm(String transCode, String merchantId,
                                     String respCode, String sysTraceNum, String merOrderNum,
                                     String orderId, String bussId, String tranAmt, String orderAmt,
                                     String bankFeeAmt, String integralAmt, String vaAmt,
                                     String bankAmt, String bankId, String integralSeq, String vaSeq,
                                     String bankSeq, String tranDateTime, String payMentTime,
                                     String settleDate, String currencyType, String orderInfo,
                                     String userId, String signValue) {
        BackForm backForm = new BackForm();
        backForm.setTransCode(transCode);
        backForm.setMerchantId(merchantId);
        backForm.setRespCode(respCode);
        backForm.setSysTraceNum(sysTraceNum);
        backForm.setMerOrderNum(merOrderNum);
        backForm.setOrderId(orderId);
        backForm.setBussId(bussId);
        backForm.setTranAmt(tranAmt);
        backForm.setOrderAmt(orderAmt);
        backForm.setBankFeeAmt(bankFeeAmt);
        backForm.setIntegralAmt(integralAmt);
        backForm.setVaAmt(vaAmt);
        backForm.setBankAmt(bankAmt);
        backForm.setBankId(bankId);
        backForm.setIntegralSeq(integralSeq);
        backForm.setVaSeq(vaSeq);
        backForm.setBankSeq(bankSeq);
        backForm.setTranDateTime(tranDateTime);
        backForm.setPayMentTime(payMentTime);
        backForm.setSettleDate(settleDate);
        backForm.setCurrencyType(currencyType);
        backForm.setOrderInfo(orderInfo);
        backForm.setUserId(userId);
        backForm.setDataKey(dataKey);
        backForm.setSignValue(signValue);
        return backForm;
    }
}
